package com.solution.dao;

import org.apache.log4j.Logger;

import com.solution.constant.ErrorMessages;
import com.solution.model.PO;

/**
 * This class is the factory for DAO implementation instances.
 * We provide a DAO interface to it and it returns the implementation instance.
 * @author piyush.mittal
 *
 */
public class DAOFactory {
	/**
	 * Logging instance.
	 */
	private static Logger log = Logger.getLogger(DAOFactory.class.getName());
	
	/**
	 * Returns the instance of the DAOImpl class
	 * based on the name of the DAO interface.
	 * @param interfaceName
	 * @return
	 */
	public static DAO getDAOInstance(String interfaceName){
		try{
			return (DAO)(Class.forName(interfaceName+"Impl").newInstance());
		}
		catch(ClassNotFoundException ex){
			throw new RuntimeException(ErrorMessages.DAO_IMPL_INSTANCE_CREATION_EXCEPTION,ex);
		}
		catch(IllegalAccessException ex){
			throw new RuntimeException(ErrorMessages.DAO_IMPL_INSTANCE_CREATION_EXCEPTION,ex);
		}
		catch(InstantiationException ex){
			throw new RuntimeException(ErrorMessages.DAO_IMPL_INSTANCE_CREATION_EXCEPTION,ex);
		}
	}
	
	/**
	 * Returns the instance of the DAOImpl class
	 * based on the DAO interface class.
	 * @param interfaceName
	 * @return
	 */
	public static <S extends PO,T extends DAO<S>> T getDAOInstance(Class<T> DAOInterface){
		return (T)getDAOInstance(DAOInterface.getName());
	}
}
